package com.woniuxy.mini;

import com.woniuxy.core.service.ShopService;
import com.woniuxy.core.service.ShopTypeService;
import com.woniuxy.entity.AllShop;
import com.woniuxy.entity.DTO.PageResponseDTO;
import com.woniuxy.entity.DTO.ShopAndUserDTO;
import com.woniuxy.entity.ShopType;
import com.woniuxy.util.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author hq
 * @Date 2025/7/13$ 15:54$
 * @Description TODO
 * @Params $
 * @Return $
 **/
@Slf4j
@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private ShopService shopService;
    @Autowired
    private ShopTypeService shopTypeService;
    //添加商品
    @PostMapping( "/add")
    public ResponseData<AllShop> createShop(@RequestBody AllShop shop){
        shopService.createShop(shop);
        return ResponseData.ok(shop);
    }
    /**
     * 获取所有商品并分页
     */
    @GetMapping("/findShopPage/{pageNum}/{pageSize}")
    public ResponseData<PageResponseDTO<ShopAndUserDTO>> getAllShops(
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize,
            @RequestParam(name="title" ,required = false) String  title,
            @RequestParam(name="typeId" ,required = false) Integer typeId) {
        PageResponseDTO<ShopAndUserDTO> response = shopService.getAllShops(pageNum, pageSize, title,typeId);
        return ResponseData.ok(response);
    }
    /**
     * 删除商品
     */
    @DeleteMapping("/{id}")
    public ResponseData<Boolean> deleteShop(@PathVariable("id") Integer id) {
        shopService.deleteShop(id);
        return ResponseData.ok(true);
    }
    /**
     * 下架商品，修改商品状态state = 2
     */
    @PostMapping("/offline/{id}/{status}")
    public ResponseData<Boolean> offlineShop(@PathVariable("id") Integer id,@PathVariable("status") Integer status) {
        shopService.offlineShop(id,status);
        return ResponseData.ok(true);
    }
    /**
     * 根据商品id查询商品
     */
    @GetMapping("/findShopById/{id}")
    public AllShop getShopById(@PathVariable("id") Integer id) {
        log.info("收到查询商品请求，ID: {}", id); // 添加日志验证
        return shopService.getShopById(id);
    }
//    封装根据商品id查询商品
    @GetMapping("/findShopByIdn/{id}")
    public ResponseData<AllShop> getShopByIdn(@PathVariable("id") Integer id) {
        log.info("收到查询商品请求，ID: {}", id); // 添加日志验证
        AllShop shop = shopService.getShopById(id);
        return ResponseData.ok(shop);
    }

    /**
     * 根据用户id查询发布的有哪些商品
     */
    @GetMapping("/findShopByUserId/{userId}")
    public ResponseData<List<AllShop>> getShopByUserId(@PathVariable("userId") Integer userId) {
        List<AllShop> shops = shopService.getShopByUserId(userId);
        return ResponseData.ok(shops);
    }
    /**
     * 查询所有商品标签
     *
     * @return 标签列表
     */
    @GetMapping("/alltype")
    public ResponseData<List<ShopType>> getAllShopTypes() {
        List<ShopType> typeList = shopTypeService.list();
        log.info("查询到 {} 个商品标签", typeList.size());
        return ResponseData.ok(typeList);
    }
}
